package javaMSCRM;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.Border;

import com.microsoft.schemas.crm._2006.webservices.BusinessEntity;

/**
 * 
 */

/**
 * @author Zoran IVANOV
 *
 */
public class Form extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JavaMSCRM JavaMSCRM;
	BusinessEntity record = null;
	String EntityId = "";
	JButton SaveButton,DeleteButton;
	JPanel buttonsBar;

	/**
	 * @author Zoran IVANOV
	 *
	 */
	public class JavaMSCRMLabel extends JLabel{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		public JavaMSCRMLabel(String LabelText, Boolean bold){
			setText(LabelText+" : ");
			if(bold){
				setFont(new Font("Arial", Font.BOLD, 12));
			}else{
				setFont(new Font("Arial", Font.PLAIN, 12));
			}
			
		}
	}
	
	/**
	 * @author Zoran IVANOV
	 *
	 */
	public class JavaMSCRMComboBox extends JComboBox {
		private static final long serialVersionUID = 1L;
		String[] Items = null;

		public JavaMSCRMComboBox(String[] Items, boolean addEmptyItem) {
			setFont(new Font("Arial", Font.PLAIN, 12));
			setBackground(Color.WHITE);
			
			this.Items = Items;
			
			if(addEmptyItem){
				addItem("");
			}
			
			for(int i=0; i<this.Items.length; i++){ 
				addItem(this.Items[i]);
			}
		}

	}

	/**
	 * @param JavaMSCRM
	 */
	public Form(JavaMSCRM JavaMSCRM) {
		this.JavaMSCRM = JavaMSCRM;
		Image img = Toolkit.getDefaultToolkit().getImage("C:/Users/Zoran/workspace/JavaMSCRM/src/icon.png");
		setIconImage(img);	
		buttonsBar = new JPanel();	
		buttonsBar.setLayout(new FlowLayout(FlowLayout.LEFT));
		Border blackline = BorderFactory.createMatteBorder(1, 0, 1, 0, Color.gray);
		buttonsBar.setBorder(blackline);		
		SaveButton = new JButton("Save");
		SaveButton.setPreferredSize(new Dimension(70, 20));
		SaveButton.addActionListener(this);
		buttonsBar.add(SaveButton);
		add(buttonsBar, BorderLayout.NORTH);		
	}
	
	/**
	 * @param JavaMSCRM
	 * @param Id
	 */
	public Form(JavaMSCRM JavaMSCRM, String Id) {
		this.JavaMSCRM = JavaMSCRM;
		this.EntityId = Id;
		Image img = Toolkit.getDefaultToolkit().getImage("C:/Users/Zoran/workspace/JavaMSCRM/src/icon.png");
		setIconImage(img);		
		buttonsBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
		Border blackline = BorderFactory.createMatteBorder(1, 0, 1, 0, Color.gray);
		buttonsBar.setBorder(blackline);		
		SaveButton = new JButton("Save");
		DeleteButton = new JButton("Delete");
		SaveButton.setPreferredSize(new Dimension(70, 20));
		SaveButton.addActionListener(this);
		DeleteButton.setPreferredSize(new Dimension(70, 20));
		DeleteButton.addActionListener(this);
		buttonsBar.add(SaveButton);
		buttonsBar.add(DeleteButton);
		add(buttonsBar, BorderLayout.NORTH);
	}
	
	/**
	 * @param panel
	 * @param text
	 */
	public void addSeparator(JPanel panel, String text)
	{
		JLabel l = new JLabel(text);		
		l.setFont(new Font("Arial", Font.BOLD, 14));
		l.setForeground(new Color(0, 70, 213));
		panel.add(l, "gapbottom 1, span, split 2, alignx left");
		panel.add(new JSeparator());
	}
	
	/**
	 * 
	 */
	public void createRecord(){
	}
	/**
	 * 
	 */
	public void updateRecord(){
	}
	/**
	 * 
	 */
	public void deleteRecord(){
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource () == SaveButton){
			if(EntityId == ""){
				createRecord();
			}else{
				updateRecord();
			}
		}else if(e.getSource () == DeleteButton){
			deleteRecord();
		}
	}
}
